<?php
session_start();
include "./common.php";
header("Content-type: text/html; charset=utf-8");
$job = $_GET['job'];
!$job && $job = "view";
$guessid = $_GET['guessid'];
!$guessid && smallmessage("参数错误!");

/**
 * 设置编码
 **/


/**
 * 答题窗口
 */
if($job=="answer"){
	include("./dataconfig.php");
	$starttime = $_CONFIG['starttime'];
	$endtime = $_CONFIG['endtime'];

	if(time()<strtotime($starttime)){
		showclose("活动尚未开始，{$starttime}后才能参加！");
	}

	if(time()>strtotime($endtime)){
		showclose("活动已结束，结束时间{$endtime}！");
	}

	$rs = $db->get_one("SELECT title,id FROM ".tname("guess")." WHERE id='$guessid' AND ifanswer=0");
	!$rs && smallmessage("灯谜不存在或已被猜中！");
	if($_POST['dosubmit']==""){
		extract($rs);
	}else{
		$db->query("UPDATE ".tname("guess")." SET answernum=answernum+1 WHERE id='$guessid'");
		$answer = trim($_POST['answer']);
		$rs = $db->get_one("SELECT answer FROM ".tname("guess")." WHERE id='$guessid' AND ifanswer=0 ");
		if($answer==$rs['answer']){
			$_SESSION['guessid'] = $guessid;
			$db->query("UPDATE ".tname("guess")." SET ifanswer=1,answertime='".time()."' WHERE id='$guessid'");
			smallmessage("恭喜你，答对了！","guess.php?job=register&guessid=$guessid");
		}else{
			smallmessage("答案不正确，继续努力哦~");
		}
	}

/**
 * 答案窗口
 */
}else if($job=="view"){
	$rs = $db->get_one("SELECT answer,prompt FROM ".tname("guess")." WHERE id='$guessid' AND ifanswer=1");
	!$rs && smallmessage("灯谜不存在或尚未猜中！");
	extract($rs);
/**
 * 提示窗口
 */
}else if($job=="prompt"){
	$rs = $db->get_one("SELECT prompt FROM ".tname("guess")." WHERE id='$guessid'");
	!$rs && smallmessage("灯谜不存在");
	extract($rs);
/**
 * 登记兑奖信息窗口
 */
}else if($job=="register"){
	if($guessid != $_SESSION['guessid']) smallmessage("非法请求!");
	if(!$_POST['dosubmit']){
		
	}else{
		$amount = $db->get_one("SELECT COUNT(id) as amount FROM ".tname("guess")." WHERE identity='$_POST[identity]'");
		if($amount['amount']>0) smallmessage("每人仅限领一张票~");
		$db->query("UPDATE ".tname("guess")." SET username='$_POST[username]',identity='$_POST[identity]',phone='$_POST[phone]',answertime='".time()."' WHERE id='$guessid'");
		$_SESSION['guessid'] = "";
		showclose("登记成功！");
	}
}

include template("guess_".$job);
?>